package bid.yanhj.ssm.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import bid.yanhj.ssm.exception.CustomException;
import bid.yanhj.ssm.mapper.ItemsMapper;
import bid.yanhj.ssm.mapper.ItemsMapperCustom;
import bid.yanhj.ssm.po.Items;
import bid.yanhj.ssm.po.ItemsCustom;
import bid.yanhj.ssm.po.ItemsQueryVo;
import bid.yanhj.ssm.service.ItemsService;
/**
 * 商品管理
 * @author Administrator
 *
 */
public class ItemsServiceImpl implements ItemsService {
	
	@Autowired
	private ItemsMapperCustom itemsMapperCustom;
	@Autowired
	private ItemsMapper itemsMapper;
	
	
	@Override
	public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)
			throws Exception {
		// 通过ItemsMapperCustom对象查询数据库
		return itemsMapperCustom.findItemsList(itemsQueryVo);
	}

	@Override
	public ItemsCustom findItemsById(Integer id) throws Exception {
	
		Items items = itemsMapper.selectByPrimaryKey(id);
		 if (items == null) {
			throw  new CustomException("修改的商品信息不存在");
		}
		//中间对商品信息进行业务处理
		//....
		//返回ItemsCustom
		ItemsCustom itemsCustom = null;
		//将items的属性值拷贝到itemsCustom
		
		if (items != null) {
			itemsCustom = new ItemsCustom();
			BeanUtils.copyProperties(items, itemsCustom);
		}
		return itemsCustom;
	}

	@Override
	public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception {
		// 添加业务的校验  通常在service中进行校验
		//int 不好判断空
		//校验id 是否为空  如果为空就 抛出异常
		
		//更新商品信息
		//更新商品信息使用updateByPrimaryKeyWithBLOBs根据id更新items表中所有字段，包括 大文本类型字段
		
		//updateByPrimaryKeyWithBLOBs要求必须转入id
		itemsCustom.setId(id);
		itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);
	}

}
